package com.tyqx.detectcputemperaturesendmessage;


import com.profesorfalken.jsensors.JSensors;
import com.profesorfalken.jsensors.model.components.Components;
import com.profesorfalken.jsensors.model.components.Gpu;
import com.profesorfalken.jsensors.model.sensors.Fan;
import com.profesorfalken.jsensors.model.sensors.Temperature;
import com.sun.mail.smtp.SMTPTransport;
import com.tyqx.detectcputemperaturesendmessage.util.AnalysisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class SchedulerMainServerSendMessage {

    private static final Logger logger = LoggerFactory.getLogger(SchedulerMainServerSendMessage.class);


    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static final SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");


    @Autowired
    private Constant constant;

    @Resource
    private AnalysisUtil analysisUtil;


    //表示每隔秒
    @Scheduled(fixedRate = 60 * 1000 * 1)
    //@Scheduled(fixedDelay = 1000 * 60 * 1)
    public void testTasks() throws Exception {
        logger.info("向主服务器发送数据定时器开始执行："+dateFormat.format(new Date())+"\n");
        try{
            RestTemplate restTemplate = new RestTemplate();
            Map<String, String> params = new HashMap<>();
            params.put("data", constant.main_server_send_data);
            ResponseEntity<String> forEntity = restTemplate.getForEntity(constant.main_server_url+"?data={data}", String.class, params);
            String body = forEntity.getBody();
            logger.info("返回结果："+body);
        }catch (Exception e){
            logger.error("给主服务器发送消息异常，主服务器路径："+constant.main_server_url);
            e.printStackTrace();
        }

        logger.info("向主服务器发送数据定时器结束执行："+dateFormat.format(new Date())+"\n");
    }

    //表示每隔秒
    @Scheduled(cron = "0 30 8 * * *")
    public void testTasks1() throws Exception {
        logger.info("向主服务器发送数据PlotNum定时器开始执行："+dateFormat.format(new Date())+"\n");
        try{
            RestTemplate restTemplate = new RestTemplate();
            Map<String, String> params = new HashMap<>();
            Integer plotNum =  analysisUtil.ChiaDebugLogAnalysisUtil();
            String produceDate = dateFormat1.format(new Date());
            String sendData = "{\"computName\":\""+constant.component_name+"\",\"plotNum\":"+plotNum+",\"produceDate\":\""+produceDate+"\"}";
            params.put("data", sendData);
            ResponseEntity<String> forEntity = restTemplate.getForEntity(constant.send_plot_num_server_url+"?data={data}", String.class, params);
            String body = forEntity.getBody();
            logger.info("返回结果："+body);
        }catch (Exception e){
            e.printStackTrace();
        }

        logger.info("向主服务器发送数据PlotNum定时器结束执行："+dateFormat.format(new Date())+"\n");
    }
}